package com.ruoyi.voucher.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.voucher.mapper.VoucherWordMapper;
import com.ruoyi.voucher.domain.VoucherWord;
import com.ruoyi.voucher.service.IVoucherWordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 凭证字Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-25
 */
@Service
public class VoucherWordServiceImpl implements IVoucherWordService 
{
    @Autowired
    private VoucherWordMapper voucherWordMapper;

    /**
     * 查询凭证字
     * 
     * @param wordId 凭证字主键
     * @return 凭证字
     */
    @Override
    public VoucherWord selectVoucherWordByWordId(Long wordId)
    {
        return voucherWordMapper.selectVoucherWordByWordId(wordId);
    }

    /**
     * 查询凭证字列表
     * 
     * @param voucherWord 凭证字
     * @return 凭证字
     */
    @Override
    public List<VoucherWord> selectVoucherWordList(VoucherWord voucherWord)
    {
        return voucherWordMapper.selectVoucherWordList(voucherWord);
    }

    /**
     * 新增凭证字
     * 
     * @param voucherWord 凭证字
     * @return 结果
     */
    @Override
    public int insertVoucherWord(VoucherWord voucherWord)
    {

        Integer isDefault = voucherWord.getIsDefault();
        if (isDefault == 2) {
            return voucherWordMapper.insert(voucherWord);
        } else if (isDefault == 1) {
            List<VoucherWord> voucherWords = voucherWordMapper.selectList(new QueryWrapper<VoucherWord>().lambda().eq(VoucherWord::getIsDefault, 1));
            for (VoucherWord word : voucherWords) {
                word.setIsDefault(2);
                voucherWordMapper.updateById(word);
            }
            return voucherWordMapper.insert(voucherWord);
        } else {
            throw new RuntimeException("isDefault的值只能是默认或非默认");
        }
    }

    /**
     * 修改凭证字
     * 
     * @param voucherWord 凭证字
     * @return 结果
     */
    @Override
    @Transactional
    public int updateVoucherWord(VoucherWord voucherWord)
    {
        //1.首先要判断传进来的是否是默认的，如果不是，则修改默认的为2，如果是，则修改为1
        VoucherWord voucherWord1 = voucherWordMapper.selectById(voucherWord.getWordId());
        if (voucherWord.getIsDefault().equals(voucherWord1.getIsDefault())) {
            return voucherWordMapper.updateById(voucherWord);
        } else {
            if (voucherWord.getIsDefault().equals(1)) {
                List<VoucherWord> voucherWords = voucherWordMapper.selectList(new QueryWrapper<VoucherWord>().lambda().eq(VoucherWord::getIsDefault, 1));
                for (VoucherWord word : voucherWords) {
                    word.setIsDefault(2);
                    voucherWordMapper.updateById(word);
                }
                return voucherWordMapper.updateById(voucherWord);
            } else {
                throw new RuntimeException("当前是默认不可被修改");
            }
        }
    }


    /**
     * 批量删除凭证字
     * 
     * @param wordIds 需要删除的凭证字主键
     * @return 结果
     */
    @Override
    public int deleteVoucherWordByWordIds(Long[] wordIds)
    {
        int a = 0;
        for (Long wordId : wordIds) {
            voucherWordMapper.updateVoucherWord(VoucherWord.builder().isDeleted(0).wordId(wordId).build());
            a++;
        }
//        voucherWordMapper.deleteVoucherWordByWordIds(wordIds);
        return a;
    }

    /**
     * 删除凭证字信息
     * 
     * @param wordId 凭证字主键
     * @return 结果
     */
    @Override
    public int deleteVoucherWordByWordId(Long wordId)
    {
        return voucherWordMapper.deleteVoucherWordByWordId(wordId);
    }

    @Override
    public List<VoucherWord> getVoucherWordAll() {
        return voucherWordMapper.selectList(null);
    }
}
